package com.topdt.jbpm.web.struts.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.thx.common.struts2.CurdAction;
import com.thx.common.util.ThreadLocalContext;
import com.thx.common.web.Page;
import com.topdt.jbpm.identity.IdFactory;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.User;
import com.topdt.jbpm.model.Todo;
import com.topdt.jbpm.model.Work;
import com.topdt.jbpm.service.TodoService;
import com.topdt.jbpm.service.WorkService;
import com.topdt.jbpm.web.SessionUserUtil;

@Namespace(value = "/workflow")
@Action(value = "todo",
		results = {
			@Result(name = "list", location = "/workflow/todo/list.jsp"),
			@Result(name = "list_portal", location = "/workflow/todo/list_portal.jsp"),
			@Result(name = "list_oneApp", location = "/workflow/todo/list_oneApp.jsp"),
			@Result(name = "list_lending", location = "/workflow/todo/list_lending.jsp"),
			@Result(name = "allTodoList", location = "/workflow/todo/myTodoList.jsp"),
		},
		interceptorRefs = {
			@InterceptorRef("topDtStack"),
			@InterceptorRef(value = "pageInterceptor", params = {"includeMethods", "doListByAppId"}),
			@InterceptorRef(value = "pageInterceptor", params = {"includeMethods", "allTodoList"})
		}
)
@Controller
@Scope("prototype")
public class TodoAction extends CurdAction<Todo> {

	private static final long serialVersionUID = 1L;

	private IdentityService identityService = IdFactory.getIdentityServiceFactory().getIdentityService();

	@Autowired
	private TodoService todoService;
	
	@Autowired
	private WorkService workService;

	User user  = SessionUserUtil.getCurrentUser();

	private String appId;
	private String isExpand;
	private String officeDocTypeAppIds;//fengjinshou 20151223 公文类型id
	private String startorTitle;//fengjinshou 20151230 列表上显示的发起人表头
	private String appName;//fengjinshou 20151230 应用名

	@Override
	public void doList() {
		logger.debug("<进入>:doList方法");
		List<Todo> listTemp = new ArrayList<Todo>();
		if("请输入标题名".equals(entity.getTodoTitle()) || StringUtils.isBlank(entity.getTodoTitle())){
			entity.setTodoTitle(null);
		}
		if("ticket".equals(entity.getTodoType())){
			listTemp = todoService.findByWorkId(user.getId(), appId, entity.getTodoTitle());
		}else if("document".equals(entity.getTodoType())){
			if(StringUtils.isBlank(officeDocTypeAppIds)){
				listTemp = todoService.getAll(user.getId(), entity.getTodoTitle());
			}else{//fengjinshou 20151223 根据应用范围查找
				listTemp = todoService.getAll(user.getId(), entity.getTodoTitle(), officeDocTypeAppIds);
			}
		}
		list = perfectList(listTemp);
	}
	
	/**
	 * 首页加载待办数据
	 * @return
	 */
	public String getTodoListForPortal() {
		List<Todo> listTemp = todoService.getTodoByUserId(user.getId(), 5);
		list = perfectList(listTemp);
		return "list_portal";
	}

	/**
	 * 查询用户所有待办
	 * @author 刘杰
	 * @date   2015年12月28日 下午2:31:09
	 * @return
	 */
	public String allTodoList(){
		logger.debug("<进入>:allTodoList方法   查询用户甩有待办");
		List<Todo> listTemp = new ArrayList<Todo>();
		if((StringUtils.isNotBlank(entity.getTodoTitle())&&entity.getTodoTitle().equals("请输入标题"))||entity.getTodoTitle()==null||"".equals(entity.getTodoTitle())){
			entity.setTodoTitle(null);
		}
		listTemp = todoService.getAllTodo(user.getId(),entity.getTodoTitle());
	    list = perfectList(listTemp);
		return "allTodoList";
	}

	/**
	 * 根据appId搜索
	 * @author 冯晋首
	 * @date   2015年12月21日 下午7:52:46
	 * @return
	 */
	public String doListByAppId() {
		logger.debug("<进入>:doListByAppId方法");
		List<Todo> listTemp = new ArrayList<Todo>();
		if(appId!=null && !appId.equals("") && !appId.equals("null")){
			if((StringUtils.isNotBlank(entity.getTodoTitle())&&entity.getTodoTitle().equals("请输入标题名"))||entity.getTodoTitle()==null||"".equals(entity.getTodoTitle())){
				entity.setTodoTitle(null);
			}
			if("ticket".equals(entity.getTodoType())){
				listTemp = todoService.findByWorkId(user.getId(),appId, entity.getTodoTitle());
			}else if("document".equals(entity.getTodoType())){
				listTemp = todoService.getAll(user.getId(),entity.getTodoTitle(),appId);
			}
			list = perfectList(listTemp);
		}
		if(StringUtils.isNotBlank(appName) && appName.indexOf("借阅")!=-1){
			return "list_lending";
		}
		return "list_oneApp";
	}

	/**
	 * 给listTemp中的Todo对象设置fromUser、touser、work三个对象的值
	 * @param listTemp
	 * @return
	 */
	private List<Todo> perfectList(List<Todo> listTemp){
		logger.debug("<进入>:perfectList方法");
		if(listTemp!=null && listTemp.size()>0){
			for(int i=0;i<listTemp.size();i++){
				Todo todo = listTemp.get(i);
				User fromUser = identityService.getUserById(todo.getFromUserId());
				User toUser = identityService.getUserById(todo.getToUserId());
				Work work = workService.getWorkById(todo.getWorkId());
				todo.setFormUser(fromUser);
				todo.setToUser(toUser);
				todo.setWork(work);
			}
		}
		return listTemp;
	}

	/**
	 * 根据workId查找待办
	 * @author 刘杰
	 * @date   2016年1月8日 下午8:27:05
	 */
	public void findTodosByWorkId(){
		logger.debug("<进入>:findTodosByWorkId方法   根据workId查找待办");
		try{
			List<Todo> todoList = todoService.findTodosByWorkId(entity.getWorkId());
			if(todoList.size()>0){
				for(Todo todo:todoList){
					User toUser = identityService.getUserById(todo.getToUserId());
					todo.setToUser(toUser);
				}
			}
			JSONArray jsonArray = (JSONArray) JSONArray.toJSON(todoList);
			writeJson(jsonArray.toString());
		}catch(Exception e){
			logger.error("<错误>:根据workId查找待办失败",e);
		}
	}

	public String getAppId() {
		return appId;
	}
	public void setAppId(String appId) {
		this.appId = appId;
	}
	public String getIsExpand() {
		return isExpand;
	}
	public void setIsExpand(String isExpand) {
		this.isExpand = isExpand;
	}
	public String getOfficeDocTypeAppIds() {
		return officeDocTypeAppIds;
	}
	public void setOfficeDocTypeAppIds(String officeDocTypeAppIds) {
		this.officeDocTypeAppIds = officeDocTypeAppIds;
	}

	public String getStartorTitle() {
		return startorTitle;
	}

	public void setStartorTitle(String startorTitle) {
		this.startorTitle = startorTitle;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}
}
